package com.demo.cms.action.manage;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.util.ServletContextAware;

import com.demo.Constants;
import com.demo.cms.model.Article;
import com.demo.cms.model.ArticleExtend;
import com.demo.cms.service.ArticleExtendService;
import com.demo.cms.service.ArticleService;
import com.demo.common.Utils;
import com.demo.core.model.File;
import com.demo.core.model.Folder;
import com.demo.core.model.Site;
import com.demo.core.service.DocumentService;
import com.demo.security.annotation.function.Function;
import com.demo.webapp.action.PageAction;
import com.demo.webapp.context.MultisiteContextHolder;

/**
 * 多媒体文件管理Action
 * 该多媒体文件主要由flash,音乐,视频,文件组成
 * 由文章进行管理
 * 文章媒体属性借助于文章扩展属性进行.
 * 注意,文章的图片内容,将不直接使用文章模型的图片属性字段
 * 而是由扩展属性指定的key进行查找和管理
 * @author ryuu.kk
 *
 */
@Function(id = 104500, pid = 104200, code = "manage.media")
public class MediaAction extends PageAction implements ServletContextAware {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = -8270334452607378794L;
	
	/**
	 * 栏目父级ID
	 */
	private Long pid;
	
	/**
	 * 文章ID
	 */
	private Long id;
	
	/**
	 * 文件uuid
	 */
	private String uuid;
	
	/**
	 * 文件路径
	 */
	private String path;
	
	/**
	 * 文章模型
	 */
	private Article article;
	
	/**
	 * 文章扩展属性模型
	 */
	private ArticleExtend articleExtend;
	
	/**
	 * 文章扩展属性列表
	 */
	private List<ArticleExtend> articleMediumList;
	
	/**
	 * 文章原始图列表
	 */
	private List<ArticleExtend> articleOriginalList;
	
	/**
	 * 文章扩展属性列表
	 */
	private List<File> imageList;
	
	/**
	 * 文章中文件模型
	 */
	private File file;
	
	/**
	 * 文章服务
	 */
	private ArticleService articleService;
	
	/**
	 * 文章扩展属性服务
	 */
	private ArticleExtendService articleExtendService;
	
	/**
	 * 文件服务
	 */
	private DocumentService documentService;
	
	/**
	 * Servlet上下文
	 */
	private ServletContext context;
	
	/**
	 * 多媒体文件一览表
	 * @return String
	 */
	public String list() {

		// 取得文章模型
		article = articleService.find(id);
		// 取得文章文件夹下所有文件模型
		imageList = documentService.findFileList(article.getFolderId());
		// ②、文章中图 （文章ID+类型）
		this.articleMediumList = articleExtendService.findList(id, "", Constants.ARTICLE_IMG_MEDIUM);
		// ③、原始图
		this.articleOriginalList = articleExtendService.findList(id, "", Constants.ARTICLE_IMG_ORIGINAL);
		
		return SUCCESS;
	}
	
	/**
	 * 增加文章图片
	 * @return String
	 */
	public String addphoto() {
		
		// 查找文章
		article = articleService.find(id);
		
		if (article == null) {
			this.addActionError("文章ID:" + id + "不存在,请联系管理员.");
			return INPUT;
		}
		
		// 文章文件夹部分 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		Long folderId = article.getFolderId();
		Folder folder = null;
		if (folderId == null) {
			folder = buildFolder();
			folderId = folder.getId();
			articleService.update(id, folderId);
		}
		// 创建图片文件:原始图-中图-缩略图
		addImage(folderId);
		// 在文件系统中创建图片文件
		//folder = documentService.findFolder(folderId);
		
		return NONE;
	}
	
	/**
	 * 删除图片
	 * @return String
	 */
	public String deletephoto() {
		
		List<ArticleExtend> extendsList = articleExtendService.findList(id, uuid, null);
		for (ArticleExtend extend : extendsList) {
			articleExtendService.deleteByKey(extend.getKey());
			documentService.deleteFild(extend.getKey());
		}
		return NONE;
	}
	
	/**
	 * 修改Photo属性
	 * @return
	 */
	public String editphoto() {
		List<ArticleExtend> extendsList = articleExtendService.findList(id, uuid, null);
		return SUCCESS;
	}
	/**
	 * 创建文章的Folder
	 * @return Folder
	 */
	@SuppressWarnings("unchecked")
	private Folder buildFolder() {
		// 文章de文件夹不存在,创建一个
		Long folderId;
		Folder folder = new Folder();
		Map<String, Object> config = (Map<String, Object>) context.getAttribute(com.demo.webapp.config.Constants.CONFIG);
		Folder root = (Folder) config.get(com.demo.webapp.config.Constants.PUBLISH_FOLDER_KEY);
		folder.setEnabled(true);
		folder.setName("文章的文件夹");
		// 取得站点ID
		Site site = MultisiteContextHolder.getSite();
		Long siteId = site.getId();
		folder.setSiteId(siteId);
		
		folder.setTitle(article.getShortTitle());
		folder.setPid(root.getId());
		folder.setUuid(Utils.uuid());
		folderId = documentService.createFolder(folder);
		folder.setId(folderId);

		return folder;
	}
	
	private void addImage(Long folderId) {
		// 文章文件部分 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		// 创建图片文件模型
		// file = buildFile(folderId, uuid, path);
		Long siteId = MultisiteContextHolder.getSite().getId();
		
		this.uuid = Utils.uuid();
		
		// 根据路径读取文件流
		InputStream in = context.getResourceAsStream(path);
		file = documentService.buildImage(siteId, folderId, uuid, path, in);
		
		// 数据库中持久化该File对象
		Long fileId = documentService.createFile(file);
		if (log.isDebugEnabled()){
			log.debug("create image file. id:" + fileId);
		}
		
		// 新增文章原始图
		articleExtend = new ArticleExtend();
		articleExtend.setArticleId(article.getId());
		articleExtend.setEnabled(true);
		// 使用文件的唯一性标示作为扩展属性名
		articleExtend.setKey(uuid);
		articleExtend.setKey(uuid);
		articleExtend.setValue(path);
		articleExtend.setType(Constants.ARTICLE_IMG_ORIGINAL);
		articleExtendService.add(articleExtend);
		
		// 增加文章中图 ;默认:250X150
		String mediumName = Constants.IMAGE_MEDIUM_PREFIX + path.substring(path.lastIndexOf("/") + 1);
		// file = buildMediumFile(folderId, uuid, path);
		in = context.getResourceAsStream(path);
		file = documentService.buildImage(siteId, folderId, Utils.uuid(), path, mediumName, 250, 150, in);
		// 数据库中持久化该缩略图对象
		documentService.createFile(file);
		articleExtend = new ArticleExtend();
		articleExtend.setArticleId(article.getId());
		articleExtend.setEnabled(true);
		
		// 使用文件的唯一性标示作为扩展属性名;原始图的uuid作为name
		articleExtend.setKey(uuid);
		articleExtend.setKey(file.getUuid());
		
		articleExtend.setValue(file.getPath() + mediumName);
		articleExtend.setType(Constants.ARTICLE_IMG_MEDIUM);
		articleExtendService.add(articleExtend);
		
		// 新增文章缩略图
		//file = buildThumb(folderId, uuid, path);
		in = context.getResourceAsStream(path);
		String thumbName = Constants.IMAGE_THUMB_PREFIX + path.substring(path.lastIndexOf("/") + 1);
		file = documentService.buildImage(siteId, folderId, Utils.uuid(), path, thumbName, 100, 100, in);
		// 数据库中持久化该缩略图对象
		documentService.createFile(file);
		articleExtend = new ArticleExtend();
		articleExtend.setArticleId(article.getId());
		articleExtend.setEnabled(true);
		// 使用文件的唯一性标示作为扩展属性名;原始图的uuid作为name
		articleExtend.setKey(uuid);
		articleExtend.setKey(file.getUuid());
		
		articleExtend.setValue(file.getPath() + thumbName);
		articleExtend.setType(Constants.ARTICLE_IMG_THUMBNAIL);
		articleExtendService.add(articleExtend);
	}
	// ~ settings here ---------------------------------------------------------------
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public Long getPid() {
		return pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public ArticleExtend getArticleExtend() {
		return articleExtend;
	}

	public void setArticleExtend(ArticleExtend articleExtend) {
		this.articleExtend = articleExtend;
	}

	public List<ArticleExtend> getArticleMediumList() {
		return articleMediumList;
	}

	public List<ArticleExtend> getArticleOriginalList() {
		return articleOriginalList;
	}

	public List<File> getImageList() {
		return imageList;
	}

	public void setImageList(List<File> imageList) {
		this.imageList = imageList;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}



	public void setArticleExtendService(ArticleExtendService articleExtendService) {
		this.articleExtendService = articleExtendService;
	}

	public void setDocumentService(DocumentService documentService) {
		this.documentService = documentService;
	}

	@Override
	public void setServletContext(ServletContext context) {
		this.context = context;
	}
}
